﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;

namespace ProjectBase.Core
{
    /// <summary>
    ///     Extensions available on every place of the framework.
    /// </summary>
    public static partial class Ext
    {
        #region Type GetDescriptors
        /// <summary>
        ///     returns (Cached) information about "item" properties
        /// </summary>
        /// <returns></returns>
        public static IDictionary<string, PropertyDescriptor> GetPropertyDescriptors(this Type type)
        {
            Contract.Requires(type.Is(), "Type cannot be null");
            return TypeDescriptor.GetProperties(type)
                .Cast<PropertyDescriptor>()
                .ToDictionary(
                    pd => pd.Name,
                    pd => pd);
        }

        public static bool HasType(this Type toBeCompared, Type type)
        {
            if (toBeCompared.IsNull()
                || type.IsNull())
            {
                return false;
            }
            if (toBeCompared.IsEquivalentTo(type))
            {
                return true;
            }
            if (type.IsInterface)
            {
                return toBeCompared.GetInterfaces().Any(i => i.IsEquivalentTo(type));
            }
            return false;
        }
        #endregion Type GetDescriptors
    }
}